#!D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe -q
<?php
$domain = 'http://www.jrkadmin.site';
$cfgPath = __DIR__.'/cfg.json';
$logPath = __DIR__.'/log.txt';

$config = json_decode(file_get_contents($cfgPath), true);
if(empty($config)){
	$data = date('Y-m-d H:i:s',time()) . " 错误：配置错误！\r\n";
	file_put_contents($logPath, $data, FILE_APPEND);
	return;
}

$starttime = strtotime($config['starttime']);
$endtime = strtotime($config['endtime']);

file_get_contents($domain.'/?s=api/v1.auto/dianpu&action=reset');
file_put_contents($logPath, date('Y-m-d H:i:s',time())." 启动服务！\r\n", FILE_APPEND);
while (true) {
	$sleep = mt_rand($config['sleep_s'],$config['sleep_b']);
    $time = time();
    if($time >= $starttime && $time <= $endtime){
        $output = json_decode(file_get_contents($domain.'/?s=api/v1.auto/dianpu&action=run'),true);
        if($output['status']){
            $info = date('Y-m-d H:i:s',$time)." 执行：".$output['msg']." 信息：等待".$sleep."秒\r\n";
            file_put_contents($logPath, $info, FILE_APPEND);
        }else{
            $info = date('Y-m-d H:i:s',$time)." 信息：".$output['msg']."\r\n";
            file_put_contents($logPath, $info, FILE_APPEND);
            return;
        }
    }else{
    	$info = date('Y-m-d H:i:s',$time)." 信息：今日任务结束！\r\n";
        file_put_contents($logPath, $info, FILE_APPEND);
        return;
    }
	sleep($sleep);
}

// function db_conect(){
//     $servername = '';
//     $username = '';
//     $password = '';
//     $database = '';
//     $conn = mysqli_connect($servername, $username, $password, $database);
//     if (!$conn) {
//         die("Connection failed: " . mysqli_connect_error());
//     }
//     return $conn;
// }

// function reset($conn){
//     Db::name('auto')->where('table',$param['table'])->setField('num',0);
//     $sql = '';
//     $result = mysqli_query($conn, $query);
// }